<template>
  <div class="risk">
    <eagle-tab-page-list
      :url="url"
      :params="params"
      @initActive="_initActive"
      @initList="_initList"
    >
    <div v-if="this.active === 0">
      <eagle-cell
        style="margin-top: 10px"
        v-for="(item, index) in list"
        :key="index"
      >
        <div class="warn_item">
            <div @click="goDetail(item)">
              <div class="warn_header">
                <div>
                  {{ index + 1 }}、隐患描述：{{ item.HiddenDangerDesc }}
                </div>
                <eagle-tag
                  v-if="item.ExpireDays < 0"
                  class="warn_tag"
                  type="warning"
                  >剩{{ Math.abs(item.ExpireDays) }}</eagle-tag
                >
                <eagle-tag
                  v-if="item.ExpireDays > 0"
                  class="warn_tag"
                  type="warning"
                  >逾期{{ Math.abs(item.ExpireDays) }}</eagle-tag
                >
              </div>
              <!-- <div class="dep_sort">
                <img src="@/assets/Images/AppIcon/icon_list_org.png" alt="" />
                <div>{{ item.CheckTypeName }}</div>
              </div> -->
              <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_org.png"
                    alt=""
                  />所属任务：{{ item.CheckTaskName }}</eagle-col
                >
              </eagle-row>

              <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="12">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_post.png"
                    alt=""
                  />{{
                    item.OriginType == 1
                      ? "检查表检查"
                      : item.StatusName == 2
                      ? "依据检查"
                      : "随手拍"
                  }}</eagle-col
                >
              </eagle-row>

              <eagle-row class="dep_sort">
                <eagle-col span="8">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_security.png"
                    alt=""
                  />{{ item.CreateChnName }}</eagle-col
                >
                <eagle-col span="16">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_security.png"
                    alt=""
                  />{{ item.CreateDate }}</eagle-col
                >
              </eagle-row>
            </div>
          </div>
      </eagle-cell>
    </div>
    <div v-if="this.active === 1">
      <van-checkbox-group
        ref="checkboxGroup"
        @change="handleChange"
        v-model="result"
      >
        <van-cell
          style="margin-top: 10px"
          v-for="(item, index) in list"
          :key="index"
        >
          <div class="warn_item">
            <div @click="goToDetail(item)">
              <div class="warn_header">
                <div :style="{ color: !item.IsCorrect ? 'red' : '' }">
                  {{ index + 1 }}、隐患描述：{{
                    item.HiddenDangerDesc || "暂无任何描述"
                  }}
                </div>
                <van-tag
                  v-if="item.ExpireDays < 0"
                  class="warn_tag"
                  type="warning"
                  >剩{{ Math.abs(item.ExpireDays) }}</van-tag
                >
                <van-tag
                  v-if="item.ExpireDays > 0"
                  class="warn_tag"
                  type="warning"
                  >逾期{{ Math.abs(item.ExpireDays) }}</van-tag
                >
              </div>
              <!-- <div class="dep_sort">
              <img src="@/assets/Images/AppIcon/icon_list_org.png" alt="" />
              <div>{{ item.CheckTypeName }}</div>
            </div> -->
              <van-row gutter="20" class="dep_sort">
                <van-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_org.png"
                    alt=""
                  />所属任务：{{ item.CheckTaskName }}</van-col
                >
              </van-row>

              <van-row gutter="20" class="dep_sort">
                <van-col span="12">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_post.png"
                    alt=""
                  />{{
                    item.OriginType == 1
                      ? "检查表检查"
                      : item.StatusName == 2
                      ? "依据检查"
                      : "随手拍"
                  }}</van-col
                >
              </van-row>

              <van-row class="dep_sort">
                <van-col span="8">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_security.png"
                    alt=""
                  />{{ item.CreateChnName }}</van-col
                >
                <van-col span="16">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_security.png"
                    alt=""
                  />{{ item.CreateDate }}</van-col
                >
              </van-row>
            </div>
            <template v-if="userName == 1">
              <van-divider />
              <div v-if="item.IsCorrect" class="dep_btm">
                <div class="btm_item" @click="handleAdd(item)">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_edit.png"
                    alt=""
                  />
                  <div class="mr-10">提交检查记录</div>
                </div>
              </div>
            </template>
          </div>
        </van-cell>
      </van-checkbox-group>
    </div>
    <div v-if="this.active === 2">
      <van-checkbox-group
        ref="checkboxBackGroup"
        @change="handleBackChange"
        v-model="backResult"
      >
        <van-cell
          style="margin-top: 10px"
          v-for="(item, index) in list"
          :key="index"
        >
          <div class="warn_item">
            <div @click="goBackDetail(item)">
              <div class="warn_header">
                <div :style="{ color: !item.IsCorrect ? 'red' : '' }">
                  {{ index + 1 }}、隐患描述：{{
                    item.HiddenDangerDesc || "暂无任何描述"
                  }}
                </div>
                <van-tag
                  v-if="item.ExpireDays < 0"
                  class="warn_tag"
                  type="warning"
                  >剩{{ Math.abs(item.ExpireDays) }}</van-tag
                >
                <van-tag
                  v-if="item.ExpireDays > 0"
                  class="warn_tag"
                  type="warning"
                  >逾期{{ Math.abs(item.ExpireDays) }}</van-tag
                >
              </div>
              <!-- <div class="dep_sort">
              <img src="@/assets/Images/AppIcon/icon_list_org.png" alt="" />
              <div>{{ item.CheckTypeName }}</div>
            </div> -->
              <van-row gutter="20" class="dep_sort">
                <van-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_org.png"
                    alt=""
                  />所属任务：{{ item.CheckTaskName }}</van-col
                >
              </van-row>

              <van-row gutter="20" class="dep_sort">
                <van-col span="12">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_post.png"
                    alt=""
                  />{{
                    item.OriginType == 1
                      ? "检查表检查"
                      : item.StatusName == 2
                      ? "依据检查"
                      : "随手拍"
                  }}</van-col
                >
              </van-row>

              <van-row class="dep_sort">
                <van-col span="8">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_security.png"
                    alt=""
                  />{{ item.CreateChnName }}</van-col
                >
                <van-col span="16">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_security.png"
                    alt=""
                  />{{ item.CreateDate }}</van-col
                >
              </van-row>
            </div>
            <template v-if="userName == 1">
              <van-divider />
              <div v-if="item.IsCorrect" class="dep_btm">
                <div class="btm_item" @click="handleBackAdd(item)">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_edit.png"
                    alt=""
                  />
                  <div class="mr-10">提交检查记录</div>
                </div>
              </div>
            </template>
          </div>
        </van-cell>
      </van-checkbox-group>
      <div slot="other">
        <div class="choose_btm" v-if="userName == 1">
          <div class="choose_cancel">
            <van-button
              class="mr-10"
              type="warning"
              size="small"
              @click="checkBackAll"
              >全选</van-button
            >
            <van-button
              class="mr-10"
              type="warning"
              @click="toggleBackAll"
              size="small"
              >取消全选</van-button
            >
            <div>已选择{{ backResult.length }}个项目</div>
          </div>
          <div class="choose_confirm">
            <van-button type="info" @click="backSubmit">批量提交</van-button>
          </div>
        </div>
      </div>
    </div>
    </eagle-tab-page-list>
  </div>
</template>

<script>
//任务检查记录
import { mapGetters } from "vuex";
// import { Toast } from "vant";
import { Dialog } from "vant";
export default {
  data() {
    return {
      active: 0,
      userlist: [],
      backUserList: [],
      result: [],
      backResult: [],
      finishList: [],
      selectKey: "",
      userName: this.$route.query.userName,
      url: "DangerCheckTaskDetail/GetPageData",
      list: [],
      params: [
        {
          title: "已提交的记录",
          dataType: "BeSubmited",
          ctCode: this.$route.query.ctCode,
          userName: this.$route.query.userName,
        },
        {
          title: "未提交的记录",
          dataType: "NotSubmit",
          ctCode: this.$route.query.ctCode,
          userName: this.$route.query.userName,
        },
        {
          title: "被退回的记录",
          dataType: "BeBacked",
          ctCode: this.$route.query.ctCode,
          userName: this.$route.query.userName,
        },
      ],
    };
  },
  computed: {
    ...mapGetters(["CompanyCode",'token']),
  },
  created() {
  },
  methods: {
    _initActive(active) {
      this.active = active;
    },
    _initList(list) {
      this.list = list;
    },

    checkAll() {
      this.$refs.checkboxGroup.toggleAll(true);
    },
    toggleAll() {
      this.$refs.checkboxGroup.toggleAll();
    },
    checkBackAll() {
      this.$refs.checkboxBackGroup.toggleAll(true);
    },
    toggleBackAll() {
      this.$refs.checkboxBackGroup.toggleAll();
    },

    handleChange(ids) {
      this.userlist = [];
      ids.map((item) => {
        this.list.map((innerItem, innerIndex) => {
          if (innerItem.ID == item) {
            this.userlist.push({
              ID: innerItem.ID,
              IsCorrect: innerItem.IsCorrect,
              innerIndex: innerIndex + 1,
            });
          }
        });
      });
    },

    handleBackChange(ids) {
      this.backUserList = [];
      ids.map((item) => {
        this.finishList.map((innerItem, innerIndex) => {
          if (innerItem.ID == item) {
            this.backUserList.push({
              ID: innerItem.ID,
              IsCorrect: innerItem.IsCorrect,
              innerIndex: innerIndex + 1,
            });
          }
        });
      });
    },

    submit() {
      for (let i = 0; i < this.userlist.length; i++) {
        if (!this.userlist[i].IsCorrect) {
          this.$toast(
            "第" +
              this.userlist[i].innerIndex +
              "项信息未填写完整，请先填写完后再选择"
          );
          return;
        }
      }

      let data = [];
      this.userlist.map((item, index) => {
        data.push(item.ID);
      });
      SubmitRecords(data)
        .then((res) => {
          this.$toast.success("提交成功!");
          this.getList();
          this.getMyList();
          this.getFinishList();
        })
        .catch((err) => {
          console.log("err", err);
        });
      // this.$router.push({
      //   name: "edit-chemical",
      //   params: {userlist:this.userlist},
      // });
    },

    backSubmit() {
      for (let i = 0; i < this.backUserList.length; i++) {
        if (!this.backUserList[i].IsCorrect) {
          let index = i + 1;
          this.$toast(
            "第" +
              this.backUserList[i].innerIndex +
              "项信息未填写完整，请先填写完后再选择"
          );
          return;
        }
      }
      let data = [];
      this.backUserList.map((item, index) => {
        data.push(item.ID);
      });
      SubmitRecords(data)
        .then((res) => {
          this.$toast.success("提交成功!");
          this.getList();
          this.getMyList();
          this.getFinishList();
        })
        .catch((err) => {
          console.log("err", err);
        });
    },

    handleAdd(item) {
      Dialog.confirm({
        title: "询问",
        message: "你确定要提交该检查记录吗?",
      })
        .then(() => {
          // on confirm
          handleAdd({ id: item.ID }).then((res) => {
            this.$toast.success("提交成功！");
            this.getList();
            // console.log('asdasdsad',res);
          });
        })
        .catch(() => {
          // on cancel
        });
    },

    handleBackAdd(item) {
      Dialog.confirm({
        title: "询问",
        message: "你确定要提交该检查记录吗?",
      })
        .then(() => {
          // on confirm
          handleAdd({ id: item.ID }).then((res) => {
            this.$toast.success("提交成功！");
            this.getList();
            // console.log('asdasdsad',res);
          });
        })
        .catch(() => {
          // on cancel
        });
    },

    goDetail(item) {
      this.$router.push({
        path: "/Danger/DangerCheckDetailView",
        query: { id: item.ID },
      });
    },
    // 随手拍编辑
    goToDetail(item) {
      this.$router.push({
        path: "DangerCheckDetailView",
        query: { id: item.ID },
      });
    },
    // 检查表检查编辑
    goBackDetail(item) {
      this.$router.push({
        path: "DangerCheckDetailView",
        query: { id: item.ID, type: true },
      });
    },
  },
};
</script>

<style lang="less">
.risk {
  background-color: #e8f1f8;
  .info_search {
    padding: 10px;
    border-radius: 5px !important;
    .van-field__control {
      background-color: #ffffff !important;
    }
  }
}
.all_dep {
  background-color: #ffffff;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  // display: flex;
  // flex-wrap: wrap;
  // margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.dep_btm {
  display: flex;
  justify-content: flex-end;
  .btm_item {
    display: flex;
    align-items: center;
    img {
      height: 15px;
      width: 15px;
      margin-right: 5px;
    }
  }
}

.choose_btm {
  display: flex;
  width: 100%;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  background-color: #fff;
  .choose_cancel {
    padding: 0 10px;
    display: flex;
    align-items: center;
    float: left;
  }
  .choose_confirm {
    float: right;
    border-radius: 0;
  }
}
</style>
